//
//  DiaryView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/19.
//

import SwiftUI
import Kingfisher

struct DiaryView: View {
    @State private var showFullScreen = false //预览图片
    @State private var selectedIndex = 0
    @ObservedObject var viewModel : DiaryViewModel
    
    init(bean: DiaryBean) {
        _viewModel = ObservedObject(wrappedValue: DiaryViewModel(bean: bean))
    }

    var body: some View {
        ZStack {
            VStack(alignment: .leading , spacing: 0) {
                ScrollView(.vertical) {
                    VStack(alignment: .leading, spacing: 0) {
                        Text("\n\n\(viewModel.bean.content ?? "")")
                            .font(.system(size: 14))
                            .foregroundStyle(.bgItemContent)
                            .lineSpacing(4)
                            .tracking(2.5)
                        HStack {
                            Spacer()
                            Text("---\(TimeUtils.getDate(time: viewModel.bean.time, format: "yyyy年MM月dd日"))")
                                .font(.system(size: 14))
                                .foregroundStyle(.bgItemContent)
                                .lineSpacing(4)
                                .tracking(2.5)
                        }.frame(maxWidth: .infinity)
                            .padding(.top,30)
                        Spacer()
                    }.frame(maxWidth: .infinity,maxHeight: .infinity, alignment: .leading)
                }.frame(maxWidth: .infinity)
                    .padding(.leading, 20)
                    .padding(.trailing,10)
                    .padding(.top, 10)
                    .padding(.bottom,5)
                Spacer()
                pictureView
                    .padding(.horizontal, 10)
                    .padding(.bottom,10)
            }.frame(maxWidth: .infinity)
                .background(.bgItem)
                .cornerRadius(15)
                .padding(.horizontal, 20)
                .padding(.vertical,10)
        }.navigationTitle("")
            .frame(maxWidth: .infinity)
            .background(.bg)
            .toolbar {
                // 添加一个右标题按钮
                ToolbarItem(placement: .navigationBarTrailing) {
                    Text("修改")
                        .foregroundColor(themeManager.themeColor)
                        .font(.system(size: 12))
                        .padding(.horizontal,10)
                        .padding(.vertical,7)
                        .cornerRadius(8)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            //跳转到修改日记
                            pathManager.jump(type: JumpType.ADD_DIARY, data: viewModel.bean)
                        }
                }
            }
    }
}

extension DiaryView {
    //图片列表
    var pictureView: some View {
        ScrollView(.horizontal,showsIndicators: false) {
            VStack(alignment: .leading, spacing: 0) {
                //横向布局
                HStack(spacing:7) {
                    ForEach(Array(viewModel.mediaItems.enumerated()), id: \.element.id) { index, item in
                        PictureItemView(item: item, itemWidth: 50, itemHeight: 50, cornerRadius: 15)
                            .onTapGesture {
                                selectedIndex = index
                                showFullScreen.toggle()
                            }
                    }
                }
            }
        }.fullScreenCover(isPresented: $showFullScreen) {
            FullScreenMediaView(mediaItems: viewModel.mediaItems, currentIndex: $selectedIndex)
        }.onAppear{
            if viewModel.mediaItems.isEmpty {
                viewModel.getPicture(bean: viewModel.bean)
            }
        }
    }
}

//#Preview {
//    DiaryView(bean: DiaryBean(content: "我哦",time:1739978861))
//}

//定义预览提供者
struct DiaryView_Previews: PreviewProvider {
    static var previews: some View {
        @State var bean : DiaryBean = DiaryBean(content: "日记内容")
        
        //使用预览共享配置
        configurePreviewEnvironment(for: DiaryView(bean: DiaryBean(content: "我哦",time:1739978861)))
    }
}
